<?php
session_start();
require("../include/db.php");
require("../include/class.pop3.php");
require("../include/class.matchall.php");
require("../include/class.phpmailer.php");
require("../include/sendmail.php");
?>
<table width="100%">
    <tr>
    	<td width="200" valign="top">
          Administrator option
          <UL>
           <li><a href="allcompanies.htm">All companies</a></li>
           <li><a href="report.htm">Report</a></li>
           <li><a href="scanemail.php">Scan Email</a></li>
          </UL>
   	  </td>   
   	  <td valign="top">
   	  some thing abount administrator ............................</br>
<?
function saveemail($conn,$from,$to,$subject,$body)
{
    $sql = "insert into mails(fromemail,toemail,subject,body)
         values('".($from)."','".($to)."','".($subject)."','".($body)."')"; 
    $res = mysql_query($sql, $conn);
    $err = mysql_error();
    if($err)
    {
        echo "insert record into database".$err;
    }
    else
    {
        echo "insert email success";
    }
}
function receiveemail($conn)
{

    $user="admin@uwannamail.com";
    $password="ByronStraat9";
    $apop=0;
    $pop3_connection=new pop3_class;
    $pop3_connection->hostname="mail.uwannamail.com";


    if(($error=$pop3_connection->Open())=="")
    {
        //echo "<PRE>Connected to the POP3 server &quot;$pop3_connection->hostname&quot;.</PRE>\n";
        if(($error=$pop3_connection->Login($user,$password,$apop))=="")
        {
            //echo "<PRE>User &quot;$user&quot; logged in.</PRE>\n";
            if(($error=$pop3_connection->Statistics(&$messages,&$size))=="")
            {
                //echo "<PRE>There are $messages messages in the mail box with a total of $size bytes.</PRE>\n";
                $result=$pop3_connection->ListMessages("",0);
                if(GetType($result)=="array")
                {
                    //for(Reset($result),$message=0;$message<count($result);Next($result),$message++)                  
                    //echo "<PRE>Message ",Key($result)," - ",$result[Key($result)]," bytes.</PRE>\n";
                    $result=$pop3_connection->ListMessages("",1);
                    if(GetType($result)=="array")
                    {
                        for(Reset($result),$message=0;$message<count($result);Next($result),$message++)
                        {
                        //echo "<PRE>Message ",Key($result),", Unique ID - \"",$result[Key($result)],"\"</PRE>\n";
                        if($messages>0)
                        {
                            if(($error=$pop3_connection->RetrieveMessage(Key($result),&$headers,&$body,2))=="")
                            {
                                $from="";
                                $to="";
                                $subject="";                            	
                                //echo "<PRE>Message ".Key($result).":\n---Message headers starts below---</PRE>\n";
                                for($line=0;$line<count($headers);$line++)
                                {
                                    if (substr($headers[$line],0,4)=='From')
                                    {
                                        $matchresult= MatchAll::email_match($headers[$line],true,false);
                                        $from=$matchresult[0];
                                    }
                                    if (substr($headers[$line],0,2)=='To')
                                    {
                                        $matchresult= MatchAll::email_match($headers[$line],true,false);
                                        $to=$matchresult[0];
                                    }
                                    if (substr($headers[$line],0,7)=='Subject')
                                    {
                                        $subject=substr($headers[$line],8);
                                    }
                                }
                                //echo "<PRE>---Message headers ends above---\n---Message body starts below---</PRE>\n";
                                $content="";
                                for($line=0;$line<count($body);$line++)
                                {
                                    $content.=$body[$line];
                                }
                                //echo "<PRE>---Message body ends above---</PRE>\n";
                                saveemail($conn,$from,$to,$subject,$content);
                                if(($error=$pop3_connection->DeleteMessage(Key($result)))=="")
                                {
                                    /*
                                    echo "<PRE>Marked message 1 for deletion.</PRE>\n";
                                    if(($error=$pop3_connection->ResetDeletedMessages())=="")
                                    {
                                    echo "<PRE>Resetted the list of messages to be deleted.</PRE>\n";
                                    }
                                    */
                                }
                            }
                        }
                        }
                        if($error==""
                        && ($error=$pop3_connection->Close())=="")
                        echo "<PRE>Disconnected from the POP3 server &quot;$pop3_connection->hostname&quot;.</PRE>\n";

                    }
                    else
                    $error=$result;
                }
                else
                $error=$result;
            }
        }
    }
}
function genforward($conn)
{
    $sql = "select * from mails where forwardpos is null";
    $result=mysql_query($sql,$conn);
    while   ($row=mysql_fetch_array($result))
    {
        $sqll="select distinct membermailing.memberid from mails,mailing,membermailing where mails.fromemail=mailing.fromemail and mailing.id=membermailing.mailingid and mails.id=".$row['id'];
        $result1=mysql_query($sqll,$conn);
        $user="";
        $forwardcount=0;
        while  ($row1=mysql_fetch_array($result1))
        {
            $user.=$row1['memberid'].",";
            $forwardcount++;
        }
        $sqlupdate = "update mails set forward='".$user."',forwardcount=".$forwardcount." where id=".$row['id'];
        mysql_query($sqlupdate,$conn);
    }
}
$conn=getconnection();
receiveemail($conn);
genforward($conn);
?>

   	  </td>   
   	</tr>
</table>
<?php
include "../include/foot.php";
?>